//
//  LabelColorViewController.m
//  20170207
//
//  Created by xuPeng on 2017/9/6.
//  Copyright © 2017年 一直丶黑曼巴. All rights reserved.
//
#import "Header.h"
#import "LabelColorViewController.h"

@interface LabelColorViewController ()
@property (nonatomic, strong) UILabel *lab;
@end

@implementation LabelColorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"音乐";
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, PW - 40, 40)];
    lab.center = self.view.center;
    [lab setText:@"海上钢琴师,音乐本身没有胜负!"];
    lab.textAlignment = NSTextAlignmentCenter;
    self.lab = lab;
    [self editStringColor:self.lab.text editStr:@"音乐" color:[UIColor orangeColor]];
    [self.view addSubview:self.lab];
}

- (void)viewWillAppear:(BOOL)animated{
    
}

- (void)editStringColor:(NSString *)string editStr:(NSString *)editStr color:(UIColor *)color {
    // string为整体字符串, editStr为需要修改的字符串
    NSRange range = [string rangeOfString:editStr];
    
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:string];
    
    // 设置属性修改字体颜色UIColor与大小UIFont
    [attribute addAttributes:@{NSForegroundColorAttributeName:color} range:range];
    
    self.lab.attributedText = attribute;
}
@end
